<?php
namespace app\member\controller;

use app\BaseController;
use app\member\service\RouterService;
use think\captcha\facade\Captcha;

use app\member\service\UserService;
use think\facade\Session;

class Login extends BaseController
{
    public function index()
    {
        Session::clear();
        return view();
    }

    public function getNickName(){
        try{
            $nickname = session("nickname");
            $res = AjaxReturn(0,"",$nickname);
            return json($res);
        }catch (\Throwable $ex){
            $res = AjaxReturn(-1, $ex->getMessage());
            return json($res);
        }
    }
    public function getMenu(){
        try{
            $uid = session("adid");
            $service = new RouterService();
            $res = $service->getUserMenu($uid);
            return json($res);
        }catch (\Throwable $ex){
            $res = AjaxReturn(-1, $ex->getMessage());
            return json($res);
        }
    }

    /**
     * Notes:登录
     * User: ZHY
     * DateTime: 2023/6/20 15:53
     * @param UserService $user
     * @return \think\response\Json
     */
    public function doLogin(UserService $user)
    {
        try {
            $username = request()->post("username");
            $password = request()->post("password");
            $code = request()->post("code");
            if(!Captcha::check($code)){
                $res = [
                    'code' =>-1,
                    'msg'=>'验证码错误'
                ];
                return json($res);
            }
            $res = $user->validateUser($username, $password);
            return json($res);
        } catch (\Throwable $ex) {
            $res = AjaxReturn(-1, $ex->getMessage());
            return json($res);
        }
    }


    public function doReg(UserService $service){
        try{
            $code = request()->post("code");
            if(!Captcha::check($code)){
                $res = [
                    'code' =>-1,
                    'msg'=>'验证码错误'
                ];
                return json($res);
            }

            $data = request()->post();
            $res = $service->regUser($data);
            return  json($res);
        }catch (\Throwable $ex){
            $res = AjaxReturn(-1, $ex->getMessage());
            return json($res);
        }
    }



}